package com.liukai.personalbills.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.liukai.personalbills.R;
import com.liukai.personalbills.bean.Account;
import com.liukai.personalbills.bean.AccountMessage;
import com.liukai.personalbills.bean.BillsBean;
import com.liukai.personalbills.bean.UserMessage;
import com.liukai.personalbills.fragment.BillsFragment;
import com.liukai.personalbills.fragment.CheckFragment;
import com.liukai.personalbills.retrofitInterface.AccountAPI;
import com.liukai.personalbills.util.ChangeColorUtil;
import com.liukai.personalbills.util.RetrofitManager;
import com.liukai.personalbills.util.TextUtil;
import com.liukai.personalbills.util.ToastUtil;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

    private TextView tvBills;
    private ImageView imgBills;
    private TextView tvAdd;
    private CircleImageView imgAdd;
    private TextView tvCheck;
    private ImageView imgCheck;
    private ArrayList<TextView> textViews;
    private Context context = MainActivity.this;
    private FrameLayout frameLayout;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private BillsFragment billsFragment;
    private CheckFragment checkFragment;
    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        initListener();
        setSwPage(0);

    }

    private void initData() {

//        String username = TextUtil.username;
//        if(username != null){
//            AccountAPI accountAPI = RetrofitManager.getAccountAPI();
//            Call<AccountMessage> findAllTask = accountAPI.findAll(username);
//            findAllTask.enqueue(new Callback<AccountMessage>() {
//                @Override
//                public void onResponse(Call<AccountMessage> call, Response<AccountMessage> response) {
//                    int code = response.code();
//                    if(code == HttpURLConnection.HTTP_OK){
//                        AccountMessage body = response.body();
//
////                    ToastUtil.log(context,"onResponse",response.body().toString());
//                        List<Account> accounts = body.getData();
//                        String resultCode = body.getCode();
//                        if(resultCode.equals(TextUtil.CODE_OK)){
//                            // TODO: 数据查询成功
//                            Message message = new Message();
//                            message.what = 1;
//                            handler.sendMessage(message);
//                        }else{
//                            //TODO: 数据查询失败
//                        }
//                        ToastUtil.show(context,resultCode);
//                    }else{
//                        ToastUtil.show(context,"错误:" + code);
//                    }
//                }
//
//                @Override
//                public void onFailure(Call<AccountMessage> call, Throwable t) {
//                    ToastUtil.show(context,"请求失败，请检查网络是否连接");
//                    ToastUtil.log(context,"onFailure",t.toString());
//                }
//            });
//
//        }


    }

    private void initView() {
        tvBills = findViewById(R.id.tvBills);
        imgBills = findViewById(R.id.imgBills);
        tvAdd = findViewById(R.id.tvAdd);
        imgAdd = findViewById(R.id.imgAdd);
        tvCheck = findViewById(R.id.tvCheck);
        imgCheck = findViewById(R.id.imgCheck);

        textViews = new ArrayList<>();
        textViews.add(tvBills);
        textViews.add(tvAdd);
        textViews.add(tvCheck);

        frameLayout = findViewById(R.id.frameLayout);
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

    }

    private void initListener(){
        tvBills.setOnClickListener(this);
        tvAdd.setOnClickListener(this);
        tvCheck.setOnClickListener(this);
        imgBills.setOnClickListener(this);
        imgAdd.setOnClickListener(this);
        imgCheck.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();

        switch (id){
            case R.id.tvBills :
                ChangeColorUtil.changeTextColor(textViews,tvBills,context);
                setSwPage(0);

                break;

            case R.id.imgBills :
                ChangeColorUtil.changeTextColor(textViews,tvBills,context);
                setSwPage(0);

                break;

            case R.id.tvAdd :

                break;

            case R.id.imgAdd :

                Intent intent = new Intent(MainActivity.this,AddActivity.class);
                BillsBean billsBean = new BillsBean();
                intent.putExtra("billsBean",billsBean);
                startActivityForResult(intent,TextUtil.REQ_ADDACTIVITY);
//                startActivity(intent);
                break;

            case R.id.tvCheck :
                ChangeColorUtil.changeTextColor(textViews,tvCheck,context);
                setSwPage(1);

                break;

            case R.id.imgCheck :
                ChangeColorUtil.changeTextColor(textViews,tvCheck,context);
                setSwPage(1);

                break;
        }

    }

    public void setSwPage(int i) {
        //获取FragmentManager对象
        fragmentManager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        fragmentTransaction = fragmentManager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(fragmentTransaction);
        switch (i) {
            case 0:
                if (billsFragment == null) {
                    billsFragment = new BillsFragment();
                    fragmentTransaction.add(R.id.frameLayout, billsFragment);
                } else {
                    //如果微信对应的Fragment已经实例化，则直接显示出来
                    fragmentTransaction.show(billsFragment);
                }
                break;
            case 1:
                if (checkFragment == null) {
                    checkFragment = new CheckFragment();
                    fragmentTransaction.add(R.id.frameLayout, checkFragment);
                } else {
                    //如果微信对应的Fragment已经实例化，则直接显示出来
                    fragmentTransaction.show(checkFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (billsFragment != null) {
            fragmentTransaction.hide(billsFragment);
        }
        if (checkFragment != null) {
            fragmentTransaction.hide(checkFragment);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 获取新增账单界面的返回数据
         */
        if(requestCode == TextUtil.REQ_ADDACTIVITY && resultCode == TextUtil.RES_MAINACTIVITY){
            BillsBean billsBean = (BillsBean)data.getSerializableExtra("billsBean");
            String type = billsBean.getType();
            ToastUtil.show(context,billsBean.getType());

            Message message = new Message();
            message.what = 1;
            message.obj = billsBean;
            // 将消息发送给账单界面，通知其刷新账单
            handler.sendMessage(message);

        }
    }
}
